package sdk.webview.fmc.com.fmcsdk.check.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.bean.ApprovalAcceptanceHistoryBean;
import sdk.webview.fmc.com.fmcsdk.bean.ApprovalFullLogBean;
import sdk.webview.fmc.com.fmcsdk.bean.UcWfTaskDetailBean;
import sdk.webview.fmc.com.fmcsdk.bean.WfEntityDetailBean;
import sdk.webview.fmc.com.fmcsdk.check.adapter.ApprovalAcceptanceHistoryAdapter;
import sdk.webview.fmc.com.fmcsdk.check.interfaces.ApprovalAcceptanceHistoryView;
import sdk.webview.fmc.com.fmcsdk.check.persenter.ApprovalAcceptanceHistoryPresenter;
import sdk.webview.fmc.com.fmcsdk.imagepicker.utils.Utils;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;

public class ApprovalAcceptanceHistoryActivity extends BaseActivity<ApprovalAcceptanceHistoryView, ApprovalAcceptanceHistoryPresenter> implements ApprovalAcceptanceHistoryView {
    private TextView tv_applicant, tv_application_form_type,
            tv_document_name, tv_application_instructions, tv_current_processor;
    private RecyclerView rv_history;
    private LinearLayout ll_current_processor;

    private ImageView iv_back;

    private List<ApprovalAcceptanceHistoryBean> dataList = new ArrayList<>();
    private ApprovalAcceptanceHistoryAdapter historyAdapter;

    private String ownerId, projId,wfEntityId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_approval_acceptance_history;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_DC3C26)); // 设置状态栏颜色
        }

        wfEntityId = getIntent().getStringExtra("wfEntityId");
        ownerId = getIntent().getStringExtra("ownerId");
        projId = getIntent().getStringExtra("projId");
        initView();
        initRecycler();
        initListener();
        requestData();
    }

    private void initListener() {
        iv_back.setOnClickListener(v -> {
            finish();
        });
    }

    private void requestData() {
        presenter.getUcWfTaskDetail(projId, ownerId);
        presenter.getWfEntityDetail(projId, wfEntityId);
        presenter.getApprovalFullLog(projId, wfEntityId);
    }

    private void initRecycler() {
        historyAdapter = new ApprovalAcceptanceHistoryAdapter(R.layout.item_approval_acceptance_history);
        rv_history.setLayoutManager(new LinearLayoutManager(context));
        rv_history.setAdapter(historyAdapter);
    }

    private void initView() {
        tv_applicant = findViewById(R.id.tv_applicant);
        tv_application_form_type = findViewById(R.id.tv_application_form_type);
        tv_document_name = findViewById(R.id.tv_document_name);
        tv_application_instructions = findViewById(R.id.tv_application_instructions);
        rv_history = findViewById(R.id.rv_history);
        ll_current_processor = findViewById(R.id.ll_current_processor);
        tv_current_processor = findViewById(R.id.tv_current_processor);
        iv_back = findViewById(R.id.iv_back);

    }

    @Override
    protected ApprovalAcceptanceHistoryPresenter createPresenter() {
        return new ApprovalAcceptanceHistoryPresenter();
    }

    @Override
    public void getUcWfTaskDetailSuccess(UcWfTaskDetailBean bean) {
        if (null != bean) {
            UcWfTaskDetailBean.RecordBean record = bean.getRecord();
            if (null != record) {
                String currentHandleTask = record.getCurrentHandleTask();
                if (!StringUtils.isEmpty(currentHandleTask)) {
                    ll_current_processor.setVisibility(View.VISIBLE);
                    tv_current_processor.setText(currentHandleTask);
                }

            }
        }
    }

    @Override
    public void getWfEntityDetailSuccess(WfEntityDetailBean bean) {
        if (null != bean) {
            WfEntityDetailBean.RecordBean record = bean.getRecord();
            if (null != record) {
                historyAdapter.addData(0, new ApprovalAcceptanceHistoryBean(getResources().getString(R.string.Submit_an_application), Utils.getTimeM_DHM(record.getCreateDate()), record.getCreateDesc()));
                tv_applicant.setText(record.getCreateDesc());
                tv_application_form_type.setText(record.getObjectTypeDesc());
                tv_document_name.setText(record.getContent());
                tv_application_instructions.setText(record.getSubContent());
                historyAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getApprovalFullLogSuccess(ApprovalFullLogBean bean) {
        if (null != bean) {
            List<ApprovalFullLogBean.RecordBean> records = bean.getRecords();
            if (!records.isEmpty()) {
                for (int i = 0; i < records.size(); i++) {
                    dataList.add(new ApprovalAcceptanceHistoryBean(getStatusDesc(records.get(i).getStatus()), Utils.getTimeM_DHM(records.get(i).getChangeDate()), records.get(i).getExecutorDesc()));
                }
                historyAdapter.addData(dataList);
                historyAdapter.notifyDataSetChanged();
            }

        }
    }


    private String getStatusDesc(String status) {
        String statusDesc = "";
        switch (status) {
            case "0":
                statusDesc = getResources().getString(R.string.Pending_approval);
                break;
            case "1":
                statusDesc = getResources().getString(R.string.agree);
                break;
            case "2":
                statusDesc = getResources().getString(R.string.disagree);
                break;
            case "3":
                statusDesc = getResources().getString(R.string.Cancel);
                break;
            case "4":
                statusDesc = getResources().getString(R.string.Transfer);
                break;
            case "5":
                statusDesc = getResources().getString(R.string.countersign);
                break;
        }

        return statusDesc;
    }
}
